﻿using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace HEFramework
{
    public static class ElementUISystem
    {
        [EntitySystem]
        public class ElementUIAwakeSystem : AwakeSystem<ElementUI>
        {
            protected override void Awake(ElementUI _self)
            {
            }
        }


        [EntitySystem]
        public class ElementUIUpdateSystem : UpdateSystem<ElementUI>
        {
            protected override void Update(ElementUI _self)
            {
            }
        }

        [EntitySystem]
        public class ElementUIDestroySystem : DestroySystem<ElementUI>
        {
            protected override void Destroy(ElementUI _self)
            {
                _self.Close();
            }
        }

        public static void Open(this ElementUI _self)
        {
            var uc = _self.GetSceneChild<UIComponent>();
            if (uc == null)
            {
                return;
            }

            uc.PushUIAsync(eUIFlag.Element, _ =>
            {
                _self.UIWindowBehaviour = _;
                _self.Bind();
                _self.ChangeTab(0);
            });
        }

        public static void Bind(this ElementUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.UIWindowBehaviour.GetButton("Button_Back").RegisterUpEvent(_self.Close);
            for (int i = 0; i < 5; i++)
            {
                int index = i;
                _self.UIWindowBehaviour.GetButton("Button_ElementTabItem" + index).RegisterUpEvent(() => { _self.ChangeTab(index); });
            }
        }

        public static void Update(this ElementUI _self, bool _isNew = false)
        {
            if (_self.UIWindowBehaviour == null || _self.UIWindowBehaviour.State == 0)
            {
                return;
            }

            var dc = _self.GetSceneChild<DataTableComponent>();
            var element = _self.GetSceneChild<Element>();

            if (dc == null || element == null)
            {
                return;
            }

            if (_isNew)
            {
                var language_table = dc.LoadDataTable<DRLanguage>();
                for (int i = 0; i < 5; i++)
                {
                    _self.UIWindowBehaviour.GetButton("Button_ElementTabItem" + i).GetTransform().GetChild(1).GetComponent<TMP_Text>().text = language_table.GetData(117 + i).Text;
                }

                var scroll_view = _self.UIWindowBehaviour.GetScrollView("ScrollView_ScrollViewItem");
                scroll_view.RowCount = 0;
                var skill_effect_table = dc.LoadDataTable<DRSkillEffect>();
                var element_table = dc.LoadDataTable<DRElement>();
                var element_data = element_table.GetData((int)eElement.Gold + _self.Index);
                scroll_view.ItemCallback = (_behaviour, _index) =>
                {
                    var c_level = element.Elements[element_data.ID];
                    var t_level = element_data.Levels[_index];
                    Color color = Graphics.COLOR_GRAY;
                    if (c_level >= t_level)
                    {
                        color = Graphics.COLOR_CYAN;
                    }

                    var text0 = _behaviour.GetTransform().GetChild(0).GetComponent<TMP_Text>();
                    var text1 = _behaviour.GetTransform().GetChild(1).GetComponent<TMP_Text>();
                    text0.color = color;
                    text1.color = color;
                    text0.text = skill_effect_table.GetData(element_data.SkillEffectIds[_index]).Description;
                    text1.text = $"（{c_level}/{t_level}）";
                };
                scroll_view.RowCount = element_data.Levels.Length;
            }
        }

        public static void Close(this ElementUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.UIWindowBehaviour.Hide();
            _self.UIWindowBehaviour = null;
        }

        private static void ChangeTab(this ElementUI _self, int _index)
        {
            _self.Index = _index;

            Color color_click = Graphics.COLOR_DEEP_YELLOW;
            Color color_un_click = Graphics.COLOR_GRAY;

            for (int i = 0; i < 5; i++)
            {
                if (i == _index)
                {
                    _self.UIWindowBehaviour.GetButton("Button_ElementTabItem" + i).Button.image.color = color_click;
                }
                else
                {
                    _self.UIWindowBehaviour.GetButton("Button_ElementTabItem" + i).Button.image.color = color_un_click;
                }
            }

            _self.Update(true);
        }
    }
}